<template>
	<view class="calendar-content">

		<view class="title">指令选择</view>
		<view class="uni-list">
			<view class="uni-list-cell">
				<view class="uni-list-cell-db">
					<picker @change="bindPickerChange" :value="index" :range="znamelist">
						<view class="uni-input">{{znamelist[index]}}</view>
					</picker>
				</view>
			</view>
		</view>

		<view class="uni-flex uni-row" v-if="index!=2 && index < 4">
			<view class="title">回传时间间隔</view>
			<input class="uni-input" style="width: 160upx;margin: 30upx;" type="number" v-model="parm1" placeholder="请输入回传时间间隔" />
			<view class="title2">秒</view>
		</view>

		<view class="uni-flex uni-row" v-if="index==3">
			<view class="title">回传总计次数</view>
			<input class="uni-input" style="width: 160upx;margin: 30upx;" type="number" v-model="parm2" placeholder="请输入回传次数(2～96)" />
			<view class="title2">次</view>
		</view>

		<view class="uni-flex uni-row" v-if="index==2 || index==4">
			<view class="title">回传指定日期时间</view>
			<view style="margin: 30upx;background: #FFFFFF;">
				<ruiDatePicker fields="second" start="2019-06-00 00:00:00" end="2030-12-30 23:59:59" :value="parm4" @change="bindChange"
				 @cancel="bindCancel">
				</ruiDatePicker>
			</view>
		</view>

		<view class="uni-flex uni-row" v-if="index==2">
			<view class="title">记录间隔时间</view>
			<input class="uni-input" style="width: 160upx;margin: 30upx;" type="number" v-model="parm5" placeholder="请输入记录间隔时间" />
			<view class="title2">秒</view>
		</view>
		
		<view class="uni-flex uni-row" v-if="index==8">
			<view class="title">自定义指令</view>
			<input class="uni-input" style="width: 160upx;margin: 30upx;"  v-model="parm6" placeholder="请输入自定义指令" />
		</view>

		<button type="primary" @tap="sendmsg">发送</button>

	</view>

</template>
<script>
	import ruiDatePicker from '@/components/rattenking-dtpicker/rattenking-dtpicker.vue';

	export default {
		components: {
			ruiDatePicker
		},
		data() {
			return {
				mobile: '',
				parm1: '60',
				parm2: '96',
				parm4: '',//'2019-08-21 21:00:00',
				parm5: '30',
				parm6: '',
				msg: '',
				index: 2,
				znamelist: ['设置心跳包时间间隔', '设置10点回传间隔', '设置全数上传时间', '设置指定次数上传', '设备开机时间', '立即全数上传', '取消轨迹记录', '取消关机', '自定义'],
			}
		},
		onLoad(e) {
			console.log(e.name);
			this.mobile = e.name;
			this.$okret = false;
			//2019-06-12 13:31:55
			var _format = function(number) {
				return (number < 10 ? ('0' + number) : number + '');
			};
			var date = new Date();
			this.parm4 = date.getFullYear() + "-" + _format(date.getMonth() + 1) + "-" + _format(date.getDate()) +
				" " + _format(date.getHours()) + ":" + _format(date.getMinutes()) + ":" + _format(date.getSeconds());
		},
		methods: {
			bindPickerChange: function(e) {
				console.log('picker发送选择改变，携带值为：' + e.target.value)
				this.index = e.target.value;
			},
			bindChange(e) {
				// console.log(JSON.stringify(e))
				console.log(e)
				this.parm4 = e;
			},
			bindCancel(e) {
				console.log(e)
			},
			parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
				var a = str.split(/[^0-9]/);
				return new Date(a[0], a[1], a[2], a[3], a[4], a[5]);
			},
			sendmsg: function() {
				console.log(this.index)
				if (this.index == 0) {
					this.msg = "$G97," + this.parm1;
				} else if (this.index == 1) {
					//10点回传
					//$G98,30
					this.msg = "$G98," + this.parm1;
				} else if (this.index == 2 || this.index == 4) {
					console.log(this.parm4)
					var _format = function(number) {
						return (number < 10 ? ('0' + number) : number + '');
					};
					//2019-06-12 13:31:55
					var date = this.parse(this.parm4)
					console.log("---A---" + date.getUTCHours());
					var s1 = _format(date.getUTCHours()) + _format(date.getMinutes()) + _format(date.getSeconds());
					var s2 = _format(date.getUTCDate()) + _format(date.getUTCMonth()) + _format(date.getUTCFullYear() % 100);
					console.log("---" + date.getUTCDate());
					if (this.index == 2) {
						// 设置全数上传时间
						//$G99,10,30,180000,030719
						this.msg = "$G99,10," + this.parm5 + "," + s1 + "," + s2;
					} else if (this.index == 4) {
						// 设备开机时间
						//$GON,180000,140619
						this.msg = "$GON," + s1 + "," + s2;
					}
					console.log(this.msg)
				} else if (this.index == 3) {
					// 设置指定次数上传
					if (parseInt(this.parm2) < 2 || parseInt(this.parm2) > 96) {
						uni.showToast({
							title: '回传次数需在2～96区间',
							icon: 'none',
							mask: true,
							duration: 3000
						});
						return;
					}
					if (this.parm2.length < 2) {
						this.parm2 = "0" + this.parm2;
					}
					this.msg = "$G" + this.parm2 + "," + this.parm1;
				} else if (this.index == 5) {
					this.msg = "$G99,11,0,0";
				} else if (this.index == 6) {
					this.msg = "$G99,12,0,0";
				} else if (this.index == 7) {
					this.msg = "$GOF";
				} else if (this.index == 8) {
					this.msg = this.parm6;
				}

				let _this = this;
				uni.request({
					url: this.url + '/api/feige/sendmsg',
					data: {
						"name": this.mobile,
						"msg": this.msg,
					},
					success: (res) => {
						uni.showToast({
							title: '已发送指令 > ' + _this.msg,
							icon: 'none',
							mask: true,
							duration: 3000
						});

						if (_this.index == 1 || _this.index == 3) {
							setTimeout(() => {
								_this.$okret = true;
								uni.navigateBack();
							}, 800)
							return;
						}
					},
					fail: (err) => {
						// console.log(err);
						uni.showToast({
							title: '获取记录失败！' + err,
							icon: 'none',
							mask: true,
							duration: 3000
						});
					},
					complete: () => {

					}
				});
			}
		}
	}
</script>
<style>
	button {
		margin: 30upx;
	}

	.title {
		font-size: 36upx;
		margin-top: 30upx;
		margin-left: 30upx;
	}

	.title2 {
		font-size: 36upx;
		margin-top: 40upx;
		margin-right: 30upx;
	}
</style>
